package com.su.lightthings.base

import android.app.Application
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.util.Log
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.LanguageUtils
import com.blankj.utilcode.util.NetworkUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.su.lightthings.viewmodel.ConnectViewModel
//import com.tencent.bugly.crashreport.CrashReport
import com.su.lightthings.service.BluetoothService
import com.su.lightthings.utils.Cons
import com.su.lightthings.utils.SpUtil
import com.su.lightthings.viewmodel.BleViewModel

class BaseApplication : Application() {
    private val TAG = "BaseApplication"

    companion object {
        lateinit var instance: Application
    }

    init {
        instance = this
    }

    private val netStateListener = object : NetworkUtils.OnNetworkStatusChangedListener {
        override fun onDisconnected() {
            LiveEventBus.get(Cons.LiveDataBus.NET_STATE, Boolean::class.java).post(false)
        }

        override fun onConnected(networkType: NetworkUtils.NetworkType?) {
            LiveEventBus.get(Cons.LiveDataBus.NET_STATE, Boolean::class.java).post(true)
        }
    }

    /**
     * 获取当前网络是否可用
     */
    private fun initNetworkState() {
        Thread {
            val isConnect = NetworkUtils.isAvailable()
            LiveEventBus.get(Cons.LiveDataBus.NET_STATE, Boolean::class.java).post(isConnect)
        }.start()
    }

    override fun onCreate() {
        super.onCreate()
        //获取当前网络状态
        initNetworkState()
        //注册网络状态监听
        NetworkUtils.registerNetworkStatusChangedListener(netStateListener)
        //初始化蓝牙工具类
//        BtUtil.initBle(this)
        // 绑定蓝牙服务X
        bindBleService()
        //数据库初始化
//        ObjectBox.init(this)
        ConnectViewModel.INSTANCE.getHistoryDevice()
        //Sp初始化
        SpUtil.getInstance(this, "watch")

//        CrashReport.initCrashReport(this, "9945555fb9", false)

        val spIndex = SpUtil.getData(Cons.SP.SP_LANGUAGE_INDEX, 0) as Int
        Log.d(TAG, "SP_LANGUAGE_INDEX: $spIndex")
//        LanguageUtils.applyLanguage(Cons.Language.LANGUAGE_LIST[spIndex])
        LanguageUtils.updateAppContextLanguage(Cons.Language.LANGUAGE_LIST[spIndex], null)

    }

    /**
     * 绑定蓝牙服务
     */
    private fun bindBleService() {
        Log.d(TAG, "绑定服务")
        ContextCompat.startForegroundService(this, Intent(this, BluetoothService::class.java))

        bindService(Intent(this, BluetoothService::class.java), object : ServiceConnection {
            override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                Log.d(TAG, "服务已经链接!")
                BleViewModel.INSTANCE.binder = service as BluetoothService.MyBinder
//                connectLastDevice()
            }

            override fun onServiceDisconnected(name: ComponentName?) {
                Log.d(TAG, "服务断开连接!")
                BleViewModel.INSTANCE.binder = null
            }
        }, 0)
    }
}